package testdatagenerator;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;


public class Selector<T> extends JPanel {
	private static final long serialVersionUID = 1L;
	private Vector<T> items, items1, items2;
	private MyMenu Menu1, Menu2;
	private JButton Add, Remove;
	public void Pump(Vector<T> data){
		int size = data.size();
		for(int i=0;i<size;i++){
			T temp = data.get(i);
			moveItem(temp);
		}
	}
	
	public void setEnabled(boolean enabled){
		Menu1.setEnabled(enabled);
		Menu2.setEnabled(enabled);
		Add.setEnabled(enabled);
		Remove.setEnabled(enabled);
	}
	
	public T getItem(int i){
		return items.get(i);
	}
	public void addItem(T s){
		if (s==null){ return; }
		items.add(s);
		Menu1.addItem(s);
		items1.add(s);
	}
	public void removeItem(T s){
		try {
			Menu1.removeItem(s);
			items1.remove(s);
			Menu2.removeItem(s);
			items2.remove(s);
			items.remove(s);
		} catch(Exception exc){}
	}
	public void moveItem(T s){
		if (s==null){ return; }
		Menu1.removeItem(s);
		items1.remove(s);
		Menu2.addItem(s);
		items2.add(s);
	}
	public void moveBack(T s){
		if (s==null) { return; }
		Menu2.removeItem(s);
		items2.remove(s);
		Menu1.addItem(s);
		items1.add(s);
	}
	public Selector(Vector<T> s){
		items1 = new Vector<T>();
		items2 = new Vector<T>();
		Add = new JButton("Add");
		Remove = new JButton("Remove");
		ActionListener AddL = new ActionListener(){
			@SuppressWarnings("unchecked")
			public void actionPerformed(ActionEvent arg0) {
				moveItem((T) Menu1.getSelected());	
			}
		};
		Add.addActionListener(AddL);
		ActionListener RemoveL = new ActionListener(){
			@SuppressWarnings("unchecked")
			public void actionPerformed(ActionEvent arg0) {
				moveBack((T) Menu2.getSelected());	
			}
		};
		Remove.addActionListener(RemoveL);
		items = new Vector<T>();
		setSize(600, 600);
		this.setLayout(new GridLayout(1, 3));
		JPanel panel = new JPanel();
		panel.add(Add);
		panel.add(Remove);
		Menu1 = new MyMenu();
		Menu2 = new MyMenu();
		Menu1.SetSize(200, 200);
		Menu2.SetSize(200, 200);
		add(Menu1);
		add(panel);
		add(Menu2);
		for(int i=0;i<s.size();i++){
			items.add(s.get(i));
			Menu1.addItem(s.get(i));
			items1.add(s.get(i));
		}
	}
	@SuppressWarnings("unchecked")
	public Vector<T> getSelectedItems(){
		return ((Vector<T>) items2.clone());
	}
	public void pumpStringEqu(Vector<?> objs){
		Vector<T> temp = new Vector<T>();
		for(int j=0;j<objs.size();j++){
			for(int i=0;i<items1.size();i++){
				if (items1.get(i).toString().equalsIgnoreCase(objs.get(j).toString())){
					temp.add(items1.get(i));
					break;
				}
			}
		}
		Pump(temp);
	}
}
